
var connect = function(root) {
    const queue=[root]
    if(!root)return root
    while(queue.length){
      let len=queue.length
      for(let i=1;i<=len;i++){
          let node=queue.shift()
          if(i!==len){
              node.next=queue[0]
          }
          node.left&&queue.push(node.left)
          node.right&&queue.push(node.right)
      }
    }  
    return root
  };